package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.AdParts;

/**
 * This class deals with the database operations of the entity <code>AdParts</code>
 * 
 * @author siddharthsaha
 * 
 */
public class AdPartsDatabaseOperations {

	private static Logger logger = Logger.getLogger("dotmedia.dboperations");

	public void persistAdPartsEntity(AdParts adPartsEntity) {
		logger.info("IN method persistAdPartsEntity() in AdPartsDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistStatus = databaseAccessService.persistEntity(adPartsEntity);
		if (persistStatus == 0) {
			logger.info("The AdParts entity was successfully persisted in the database");
		} else {
			logger.error("The AdParts entity was not successfully persisted in the database");
		}
		logger.info("OUT method persistAdPartsEntity() in AdPartsDatabaseOperations");
	}

	public void removeAdPartsEntity(AdParts adPartsEntity) {
		logger.info("IN method removeAdPartsEntity() in AdPartsDatabaseOperations");
		logger.info("Removing an AdParts entity with the id: " + adPartsEntity.getAdpartsId().intValue());
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeStatus = databaseAccessService.removeEntity(adPartsEntity, adPartsEntity.getAdpartsId().intValue());
		if (removeStatus == 0) {
			logger.info("The AdParts entity was successfully removed from the database");
		} else {
			logger.error("The AdParts entity was not successfully removed from the database");
		}
		logger.info("OUT method removeAdPartsEntity() in AdPartsDatabaseOperations");
	}

	public void updateAdPartsEntity(AdParts adPartsEntity) {
		logger.info("IN method updateAdPartsEntity() in AdPartsDatabaseOperations");
		String query = "SELECT adPartsEntity FROM AdParts adPartsEntity WHERE adPartsEntity.ad.adId = " + adPartsEntity.getAd().getAdId() + " AND "
				+ "adPartsEntity.partCode = '" + adPartsEntity.getPartCode() + "' AND adPartsEntity.partTypes.partTypeId = "
				+ adPartsEntity.getPartTypes().getPartTypeId().intValue();
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int index = 0; index < retrivedEntities.size(); index++) {
			AdParts currentAdPartsEntity = (AdParts) retrivedEntities.get(index);
			if (currentAdPartsEntity.getAd().getAdId().intValue() == adPartsEntity.getAd().getAdId().intValue()
					&& currentAdPartsEntity.getPartCode().equals(adPartsEntity.getPartCode())
					&& currentAdPartsEntity.getPartTypes().getPartTypeId().intValue() == adPartsEntity.getPartTypes().getPartTypeId().intValue()) {
				int updateStatus = databaseAccessService.updateEntity(currentAdPartsEntity);
				if (updateStatus == 0) {
					updated = true;
				} else {
					updated = false;
				}
				break;
			}
		}
		if (updated) {
			logger.info("The AdParts entity was successfully updated");
		} else {
			logger.error("The AdParts entity was not successfully updated");
		}
		logger.info("OUT method updateAdPartsEntity() in AdPartsDatabaseOperations");
	}

	public List<AdParts> getAllAdPartsEntities() {
		logger.info("IN method getAllAdPartsEntities() in AdPartsDatabaseOperations");
		List<AdParts> toReturnList = new ArrayList<AdParts>();
		String query = "SELECT adPartsEntity FROM AdParts adPartsEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedEntities.size(); index++) {
			AdParts currentAdPartsEntity = (AdParts) retrivedEntities.get(index);
			toReturnList.add(currentAdPartsEntity);
		}
		logger.info("OUT method getAllAdPartsEntities() in AdPartsDatabaseOperations");
		return toReturnList;
	}

	public List<AdParts> getAdPartsEntitiesByAdPartId(int adPartId) {
		logger.info("IN method getAdPartsEntitiesByAdPartsId() in AdPartsDatabaseOperations");
		List<AdParts> toReturnList = new ArrayList<AdParts>();
		String query = "SELECT adPartsEntity FROM AdParts adPartsEntity WHERE adPartsEntity.adpartsId = " + adPartId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			AdParts currentAdPartsEntity = (AdParts) retrievedEntities.get(index);
			toReturnList.add(currentAdPartsEntity);
		}
		logger.info("OUT method getAdPartsEntitiesByAdPartsId() in AdPartsDatabaseOperations");
		return toReturnList;
	}

	public List<AdParts> getAdPartsEntitiesByAdId(int adId) {
		logger.info("IN method getAdPartsEntitiesByAdId() in AdPartsDatabaseOperations");
		List<AdParts> toReturnList = new ArrayList<AdParts>();
		String query = "SELECT adPartsEntity FROM AdParts adPartsEntity WHERE adPartsEntity.ad.adId = " + adId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			AdParts currentAdPartsEntity = (AdParts) retrievedEntities.get(index);
			if (currentAdPartsEntity.getAd().getAdId().intValue() == adId) {
				toReturnList.add(currentAdPartsEntity);
			}
		}
		logger.info("OUT method getAdPartsEntitiesByAdId() in AdPartsDatabaseOperations");
		return toReturnList;
	}

	public List<AdParts> getAdPartsByPartTypesId(int partTypeId) {
		logger.info("IN method getAdPartsByPartTypesId() in AdPartsDatabaseOperations");
		List<AdParts> toReturnList = new ArrayList<AdParts>();
		String query = "SELECT adPartsEntity FROM AdParts adPartsEntity WHERE adPartsEntity.partTypes.partTypeId = " + partTypeId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			AdParts currentAdPartsEntity = (AdParts) retrievedEntities.get(index);
			if (currentAdPartsEntity.getPartTypes().getPartTypeId().intValue() == partTypeId) {
				toReturnList.add(currentAdPartsEntity);
			}
		}
		logger.info("OUT method getAdPartsByPartTypesId() in AdPartsDatabaseOperations");
		return toReturnList;
	}

}
